angular.module('tab-yuyue', [])
.controller 'TabYuyueCtrl', (Toast, $location, $localStorage, $ionicLoading, $scope, $state, $http, $window, $ionicHistory, $ionicPopup, ListDataService, $ionicScrollDelegate, ionicDatePicker, $filter, datepickerBaseConfig) ->
    now = null
    $scope.info = {}
    $scope.info.shift = 0
    $scope.info.hosName = $localStorage.getProinfo.name
    $scope.info.price = $localStorage.getProinfo.price
    $scope.info.unit = $localStorage.getProinfo.unit
    $scope.info.proId = $localStorage.getProinfo.id
    $scope.$storage = $localStorage
    initTimeRange = () ->
        now = new Date()
        if now.getHours() > 8
            now.setDate(now.getDate() + 1)
        $scope.startTime = $filter('date')(now, 'yyyy-MM-dd')
        $scope.endTime = $filter('date')(now, 'yyyy-MM-dd')
        $scope.info.charge = $localStorage.getProinfo.price
    initTimeRange()
    #获取就诊信息
    fetchVisInfo = ->
        $http.get('/visitInfo', params: {isSelect: 1})
            .success (resp) ->
                if resp.message.content.length > 0
                    $scope.infoList = resp.message.content
                    $localStorage.visinfo.name = $scope.infoList[0].customerName
                    $localStorage.visinfo.phone = $scope.infoList[0].tel
                    $localStorage.visinfo.address = $scope.infoList[0].address
                    $localStorage.visinfo.id = $scope.infoList[0].id
            .error (resp, status) ->
                Toast.showMsg("获取数据失败")

    if $localStorage.visinfo == undefined
        $localStorage.visinfo = {address: '请选择就诊信息'}
        fetchVisInfo()

    $scope.openDatePickerS = ->
        config = datepickerBaseConfig(now, Date.parse($scope.startTime))
        config.callback = (val)->
            if val
                $scope.startTime = $filter('date')(new Date(val), 'yyyy-MM-dd')
                sDate1 = Date.parse($scope.startTime)
                sDate2 = Date.parse($scope.endTime)
                dateSpan = sDate2 - sDate1
                iDays = Math.ceil(dateSpan / (24 * 3600 * 1000)) + 1
                if iDays < 1
                    $scope.endTime = $scope.startTime
                    iDays = 1
                $scope.info.charge = iDays * $scope.info.price
        ionicDatePicker.openDatePicker(config);

    $scope.openDatePickerE = ->
        config = datepickerBaseConfig(Date.parse($scope.startTime))
        config.callback = (val)->
            if val
                $scope.endTime = $filter('date')(new Date(val), 'yyyy-MM-dd')
                sDate1 = Date.parse($scope.startTime)
                sDate2 = Date.parse($scope.endTime)
                dateSpan = sDate2 - sDate1
                iDays = Math.ceil(dateSpan / (24 * 3600 * 1000)) + 1
                $scope.info.charge = iDays * $scope.info.price
        ionicDatePicker.openDatePicker(config)

    #班次选择点击变色
    $scope.clickqt = ->
        document.getElementById("qt").style.color = "#36D3D0";
        document.getElementById("bb").style.color = "#000";
        document.getElementById("yb").style.color = "#000";
        $scope.info.shift = 0;
    $scope.clickbb = ->
        document.getElementById("qt").style.color = "#000";
        document.getElementById("bb").style.color = "#36D3D0";
        document.getElementById("yb").style.color = "#000";
        $scope.info.shift = 1;
    $scope.clickyb = ->
        document.getElementById("qt").style.color = "#000";
        document.getElementById("bb").style.color = "#000";
        document.getElementById("yb").style.color = "#36D3D0";
        $scope.info.shift = 2;

    #订单提交
    $scope.commit = ->
        if $localStorage.visinfo.id == undefined || $localStorage.visinfo.id == null || $localStorage.visinfo.id == ''
            Toast.showMsg('请先添加就诊信息')
        else
            $http.post('/order/', {
                charge: $scope.info.charge
                end: $scope.endTime
                medicalId: $localStorage.visinfo.id
                note: $scope.info.remark
                productId: $scope.info.proId
                shift: $scope.info.shift
                start: $scope.startTime
            })
            .success (resp) ->
                if resp.code == 0
                    $state.go('tab.orderDetail', {id: resp.message})
                else
                    Toast.showMsg("添加数据失败")
            .error (resp, status) ->
                console.log(resp)
                Toast.showMsg("添加数据失败")

    $scope.back = ->
        $state.go('tab.home')
    $scope.goChooseInfo = ->
        $localStorage.visinfo.fromYuyue = true
        $state.go("tab.visinfo");